
package logic.forms;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import logic.beans.Disease;
import logic.beans.Symptom;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import controller.SEISaudeDefaultFacade;

public class DiseaseForm extends ElementForm<Disease> {
	private static final long serialVersionUID = 1L;
	
	@Override
	public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		if (isFieldEmpty(getElement().getName())) {
			errors.add("name",new ActionMessage("disease.register.error.name.empty"));
		}
		
//		if (isFieldEmpty(getElement().getDescription())) {
//			errors.add("description",new ActionMessage("disease.register.error.description.empty"));
//		}
		
//		if (isFieldEmpty(getElement().getSource())) {
//			errors.add("source",new ActionMessage("disease.register.error.source.empty"));
//		}
		
//		if (isFieldEmpty(getElement().getOrigin())) {
//			errors.add("origin",new ActionMessage("disease.register.error.origin.empty"));
//		}
		
		return errors;
	}
	
	@Override
	public List<String> getEmptyPropertyNames() {
		List<String> result = new ArrayList<String>();
		
		if (isFieldEmpty(getElement().getName()))
			result.add("name");
		if (isFieldEmpty(getElement().getDescription()))
			result.add("description");
		if (isFieldEmpty(getElement().getSource()))
			result.add("source");
		if (isFieldEmpty(getElement().getOrigin()))
			result.add("origin");
		if (getElement().getSymptoms().isEmpty())
			result.add("symptoms");
		
		return result;
	}
	
	@Override
	public void setElementFromRequest(HttpServletRequest request) {
		SEISaudeDefaultFacade<Symptom,Long> symptomFacade = SEISaudeDefaultFacade.getInstance(Symptom.class);
		Set<Symptom> symptomList = new HashSet<Symptom>();
		String sizeParam = request.getParameter("comboListSize");
		for (int i = 0; i < Integer.parseInt(sizeParam == null ? "0" : sizeParam); i++) {
			String eachComboValue = request.getParameter("symptomsCombo" + String.valueOf(i));
			if (!eachComboValue.equals("null")) {
				symptomList.add(symptomFacade.getElementById(Long.parseLong(eachComboValue)));
			}
		}
		getElement().setSymptoms(symptomList);
	}
	
	@Override
	public void setRequestFromElement(HttpServletRequest request) {
		request.setAttribute("symptomList",SEISaudeDefaultFacade.getInstance(Symptom.class).getAllElements());
	}
}
